package com.todod.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.todod.annotaion.NotLogging;
import com.todod.common.util.ESData;
import com.todod.dto.ESCountDto;
import com.todod.dto.EsSortDto;
import com.todod.entity.EsQuery;
import com.todod.entity.Page;
import com.todod.entity.ResultData;
import com.todod.exception.CustomException;
import com.todod.model.Dict;
import com.todod.model.User;
import com.todod.service.ESClientSearchService;
import com.todod.service.IDictService;
import com.todod.service.IUserService;

/**
 * es 搜索控制器
 * 
 * @author hyw
 *
 */
@RestController
@RequestMapping("/client/search")

public class ESSearchController {
	@Autowired
	private ESClientSearchService clientSearchService;

	@Autowired
	private IDictService dictService;
	@Autowired
	private IUserService iUserService;

	@RequestMapping("/getDictEntry")
	@NotLogging
	public ResultData getSysDictListByCode(String type) {
		List<Dict> dictLists = dictService.getListByParentCode(type);
		return ResultData.success("ok", dictLists);
	}

	/**
	 * 获取搜索文档数
	 * 
	 * @param key
	 * @return
	 */
	@RequestMapping("/countData")
	public ResultData countData(String key) {
		if (!StringUtils.isNotBlank(key)) {
			throw new CustomException("请输入关键字");
		}
		try {
			Map<String, Object> map = new HashMap<>();
			ArrayList<ESCountDto> rlist = new ArrayList<ESCountDto>();
			// 部门文档数量
			String zjindex = ESData.BMWD_INDEX;
			long zjcount = clientSearchService.queryCount(zjindex, key);
			if (zjcount >= 0) {
				ESCountDto countDto = new ESCountDto(zjindex, "部门文档数量", zjcount);
				rlist.add(countDto);
				map.put("bmwd", countDto);
			}
			// 个人文档数量
			String jgindex = ESData.GRWD_INDEX;
			long jgcount = clientSearchService.queryCount(jgindex, key);
			if (jgcount >= 0) {
				ESCountDto countDto = new ESCountDto(jgindex, "个人文档数量", jgcount);
				rlist.add(countDto);
				map.put("grwd", countDto);
			}

			return ResultData.success("ok", map);
		} catch (Exception e) {

			return ResultData.error(e.getMessage());
		}

	}

	/**
	 * s索引文档总数
	 * 
	 * @return
	 * @throws Exception
	 */
	@NotLogging
	@RequestMapping("/total")
	public ResultData total() throws Exception {
		HashMap<String, Long> map = new HashMap<String, Long>();
		map.put(ESData.BMWD_INDEX, clientSearchService.total(ESData.BMWD_INDEX));
		map.put(ESData.GRWD_INDEX, clientSearchService.total(ESData.GRWD_INDEX));
		return ResultData.success("ok", map);
	}

	/**
	 * 部门文档排序
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/sortBmwd")
	public ResultData sortbmwd(String key) throws Exception {
		if (!StringUtils.isNotBlank(key)) {
			throw new CustomException("请输入关键字");
		}
		List<EsSortDto> sortfield = clientSearchService.sortfield(ESData.BMWD_INDEX, key, "guobie");
		return ResultData.success("ok", sortfield);
	}

	/**
	 * 部门文档
	 * 
	 * @Title: searchBmwd
	 * @Description:
	 * @param esQuery
	 * @return
	 */
	@PostMapping("/searchBmwd")
	public ResultData searchBmwd(@RequestAttribute Long _userId, @RequestBody EsQuery esQuery) {
		try {
			User user = iUserService.getById(_userId);
			Page<Map<String, Object>> page = clientSearchService.queryBmwd(user.getDepartid(), esQuery);

			return ResultData.success("ok", page);
		} catch (Exception e) {

			return ResultData.error(e.getMessage());
		}
	}

	/**
	 * 个人文档排序
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/sortGrwd")
	public ResultData sortgrwd(String key) throws Exception {
		if (!StringUtils.isNotBlank(key)) {
			throw new CustomException("请输入关键字");
		}
		List<EsSortDto> sortfield = clientSearchService.sortfield(ESData.GRWD_INDEX, key, "jgGuobie");
		return ResultData.success("ok", sortfield);
	}

	/**
	 * 个人文档查询
	 * 
	 * @Title: searchHdk
	 * @Description:
	 * @author gsh
	 * @date 2020年5月26日
	 * @param esQuery
	 * @return
	 */
	@PostMapping("/searchGrwd")
	public ResultData searchGrwd(@RequestAttribute Long _userId, @RequestBody EsQuery esQuery) {
		try {
			User user = iUserService.getById(_userId);
			Page<Map<String, Object>> page = clientSearchService.queryGrwd(_userId, user.getDepartid(), esQuery);

			return ResultData.success("ok", page);
		} catch (Exception e) {
			return ResultData.error(e.getMessage());
		}
	}

	/**
	 * 【首页】-
	 * 
	 * @Title: searchBmwd
	 * @Description:
	 * @param esQuery
	 * @return
	 */
	@PostMapping("/searchTopNumber")
	public ResultData searchTopNumber(@RequestAttribute Long _userId) {
		try {
			User user = iUserService.getById(_userId);
			Map<String, Object> map = clientSearchService.searchTopNumber(user.getDepartid());

			return ResultData.success("ok", map);
		} catch (Exception e) {

			return ResultData.error(e.getMessage());
		}
	}

}
